package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }


    /*
            接收前台发送的json参数，需要在方法的形参位置声明:
                    Map 或 Bean


           UserGroup(id=null, userGroupName=分群1, conditionJsonStr=null,
           tagConditions=[
                TagCondition(tagCode=TAG_POPULATION_ATTRIBUTES_NATURE_GENDER, tagName=性别,
                operatorName=等于, operator=eq, tagValues=[女性, 男性],
                tagCodePath=[TAG_POPULATION_ATTRIBUTES, TAG_POPULATION_ATTRIBUTES_NATURE, TAG_POPULATION_ATTRIBUTES_NATURE_GENDER]),
                 TagCondition(tagCode=TAG_POPULATION_ATTRIBUTES_NATURE_PERIOD, tagName=年代, operatorName=包含, operator=in,
                  tagValues=[00后, 10后], tagCodePath=[TAG_POPULATION_ATTRIBUTES, TAG_POPULATION_ATTRIBUTES_NATURE,
                  TAG_POPULATION_ATTRIBUTES_NATURE_PERIOD]), TagCondition(tagCode=TAG_CONSUMER_BEHAVIOR_ORDER_AMOUNT_7D,
                   tagName=最新7日下单金额, operatorName=大于, operator=gt, tagValues=[200], tagCodePath=[TAG_CONSUMER_BEHAVIOR,
                    TAG_CONSUMER_BEHAVIOR_ORDER, TAG_CONSUMER_BEHAVIOR_ORDER_AMOUNT_7D])],

            conditionComment=null, userGroupNum=null,
                     updateType=, userGroupComment=, updateTime=null, createTime=null, busiDate=2020-06-14)

     */
    @PostMapping(value = "/user-group")
    public Object handleUserGroup(@RequestBody UserGroup userGroup){

        //保存分群的定义到mysql //存储分群的定义信息(元数据)到数据库
        userGroupService.saveUserGroupToMysql(userGroup);

        //计算人群包人数
        UserGroup userGroup1 = userGroupService.countUserGroupNum(userGroup);

        //更新人数到mysql
        userGroupService.saveOrUpdate(userGroup1);

        //抛异常,让前台程序卡住
        //int i = 1 / 0 ;

        return  "success";

    }


    //预估人数
    @PostMapping(value = "/user-group-evaluate")
    public Object handleEvaluateNum(@RequestBody UserGroup userGroup){

        Long num = userGroupService.evaluateNum(userGroup);

        return num;

    }


    @PostMapping(value = "/user-group-refresh/{id}")
    public Object updateUserGroup(String busiDate,@PathVariable("id") String id){


        //查询这个id所对应的分群信息的元数据
        UserGroup userGroup = userGroupService.getById(id);
        //从数据库中读取的 List<TagCondition> tagConditions 的json格式，转换为对象
        String conditionJsonStr = userGroup.getConditionJsonStr();
        List<TagCondition> tagConditions = JSON.parseArray(conditionJsonStr, TagCondition.class);
        userGroup.setTagConditions(tagConditions);

        //改修改时间
        userGroup.setUpdateTime(new Date());
        //改新的人群的人数 需要计算

        //更新ck
        //删除旧的信息
        //写入新的信息
        userGroup.setBusiDate(busiDate);
        userGroupService.updateUserGroupInCK(userGroup);

        //更新redis
        Long num = userGroupService.saveUserGroupToRedis(userGroup);
        userGroup.setUserGroupNum(num);

        //更新Mysql
        userGroupService.saveOrUpdate(userGroup);

        return "success";

    }





}

